<template>
  <div>
    <div id="search">
      <el-form :inline="true" :model="TemplateForm" class="demo-form-inline">
        <el-form-item label="课程名称">
          <el-input v-model="TemplateForm.fileName" placeholder="请输入模版名字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"  icon="el-icon-search" @click="onSearchSubmit">搜索</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div id="role-table">
      <el-table
          :header-cell-style="{background:'rgb(242, 242, 242)'}"
          style="width: 100%;
          text-align: center"
          :data="tableData"
          :row-style="{height: '50px'}"
          :cell-style="{padding: '0'}"
      >
        <el-table-column label="模版名字">
          <template slot-scope="scope">
            <span>{{ scope.row.fileName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="存储名字" width="450">
          <template slot-scope="scope">
            <span>{{ scope.row.uuidName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="存储大小">
          <template slot-scope="scope">
            <span>{{ scope.row.size}}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" >
          <template slot-scope="scope">
            <span>{{ scope.row.createTime}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                icon="el-icon-download" type="primary"  size="mini"
                @click="down(scope.row)">
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div id="nav-page">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="pageSizes"
          :current-page="TemplateForm.page"
          :page-size="TemplateForm.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import instance from "@/request/request";
import {downtemplate} from "@/request/api";
import fileDownload from "js-file-download";

export default {
  data() {
    return {
      tableData: [],
      pageSizes: [3, 5, 10, 15],
      total: 0,
      TemplateForm: {  // 搜索数据
        page: 1,
        limit: 5
      },
    }
  },
  created() {
    this.gettemplateList()
  },
  methods: {
    //获取模版信息
    gettemplateList() {
      instance.get("/template/getlist",{params:this.TemplateForm}).then(res=>{
        if(res.code==200){
          this.tableData=res.data.template
          this.total=res.data.total
          return
        }
        this.$message.error(res.msg)
      }).catch(erro=>{
        this.$message.error("请求失败！")
      })
    },
    down(row){
      downtemplate(row.uuidName).then(res=>{
        fileDownload(res, row.fileName+".xlsx"); // 使用js-file-download来接收二进制流然后下载
      })
    },
    // 搜索角色
    onSearchSubmit() {
      this.TemplateForm.page = 1
      this.TemplateForm.limit = 5
      this.gettemplateList()
    },
    // 改变每页显示的条数触发
    handleSizeChange(val) {
      this.TemplateForm.limit = val;
      this.gettemplateList()
    },
    // 改变页码触发
    handleCurrentChange(val) {
      this.TemplateForm.page = val;
      this.gettemplateList()
    },

  },
}
</script>

<style scoped>
#search {
  width: 100%;
  height: 50px;
  border-bottom: 1px solid #eee;
}

#search, #header-bth, #nav-page {
  margin: 15px;
}

#role-table {
  width: 100%;
}
</style>
